package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrKeyVo;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.rmi.dgc.Lease;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;


    @Autowired
    private GraphInfoService graphInfoService;


    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;


    @Override
    public IPage<RoomItemVo> getPageInfo(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        return baseMapper.getPageList(roomItemVoPage, queryVo);
    }

    /**
     * 查询房间详细信息
     *
     * @param id
     * @return
     */
    @Override
    public RoomDetailVo getRoomInfoById(Long id) {
        //调用自带的方法,获得房间的信息（表头以及基本数据）
        RoomInfo roomInfo = getById(id);
        //其他相关联的表的数据获取
        //当前房间所属的公寓
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
//      查询中间表的数据
        List<GraphVo> graphInfoList = graphInfoMapper.getGraphInfoByIdAndType(id, ItemType.ROOM);
        //属性的列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.getattValueVoAndId(id);
        //配套信息的列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getFacilityInfoById(id);
        //标签信息的列表
        List<LabelInfo> labelInfoList = labelInfoMapper.getLabelInfoListById(id);
        //支付方式
        List<PaymentType> paymentTypeList = paymentTypeMapper.getPaymentTypeListById(id);
        //可选租期
        List<LeaseTerm> leaseTermList = leaseTermMapper.getLeaseTermListById(id);
        //创建一个返回对象
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //赋值房间属性
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        //将上面的放到对象里
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphInfoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }

    @Override
    public void removeRoomInfoById(Long id) {
        //要删除房间信息要先查看房间里有没有人，查看租约信息表有没有还在
        LambdaQueryWrapper<LeaseAgreement> leaseAgreementLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<LeaseAgreement> in = leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getRoomId, id)
                .in(LeaseAgreement::getStatus, 2, 5, 7);
        Long aLong = leaseAgreementMapper.selectCount(in);
        if (aLong > 0) {
            //房间有人不能删
            throw new LeaseException(ResultCodeEnum.HAS_PERSON);
        }
        //调用本身的方法先删除最前面的那张表
        removeById(id);
        //接下来删除相关联的其他表
        //删除图片
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<GraphInfo> eq = graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id)
                .eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoMapper.delete(eq);

        //删除属性列表
        LambdaQueryWrapper<RoomAttrValue> attrValueVoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<RoomAttrValue> eq1 = attrValueVoLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(eq1);

        //删除配套信息列表
        LambdaQueryWrapper<RoomFacility> facilityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<RoomFacility> eq2 = facilityInfoLambdaQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(eq2);

        //删除标签信息列表
        LambdaQueryWrapper<RoomLabel> labelInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<RoomLabel> eq3 = labelInfoLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(eq3);

        //删除支付方式
        LambdaQueryWrapper<RoomPaymentType> paymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<RoomPaymentType> eq4 = paymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(eq4);

        //删除可选租期
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<RoomLeaseTerm> eq5 = roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(eq5);
    }

    @Override
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {
        //先删除 在添加 就是更新
        //先获取条件
        Long id = roomSubmitVo.getId();
        //判断条件空就是添加
        if (id != null) {
            //如果条件里面有id就是更新，先删除在添加，if里面写删除
            //删除中间表的数据
            //图片
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            LambdaQueryWrapper<GraphInfo> eq = graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id)
                    .eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoMapper.delete(eq);
            //属性
            roomAttrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
            //3.根据房间id删除配套信息
            roomFacilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
            //4.根据房间id删除标签信息
            roomLabelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
            //5.根据房间id删除支付方式
            roomPaymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
            //6.根据房间id删除可选租期
            roomLeaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));
        }
        //现在向中间表里添加数据
        //图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        //验证空
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfos = new ArrayList<>();
            //遍历集合
            for (GraphVo graphVo : graphVoList) {
//                创建一个图片对象，跟着遍历走，一圈就一个
                GraphInfo graphInfo = new GraphInfo();
                //放东西
                //写死的，，房间
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemId(roomSubmitVo.getId());//前端过来的id
                graphInfos.add(graphInfo);
            }
            //循环出来graphInfos就是满的
            //批量插入的方法
            graphInfoService.saveBatch(graphInfos);
        }

        //属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)) {
            ArrayList<RoomAttrValue> roomAttrValues = new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValueBuilder = RoomAttrValue.builder().roomId(roomSubmitVo.getId())
                        .attrValueId(attrValueId).build();
                roomAttrValues.add(roomAttrValueBuilder);
            }
            roomAttrValueService.saveBatch(roomAttrValues);
        }

        //配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)) {
            ArrayList<RoomFacility> roomFacilities = new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility build = RoomFacility.builder().facilityId(facilityInfoId).roomId(roomSubmitVo.getId()).build();
                roomFacilities.add(build);
            }
            roomFacilityService.saveBatch(roomFacilities);
        }
        //标签
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)) {
            ArrayList<RoomLabel> labelInfos = new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabelBuilder = RoomLabel.builder().labelId(labelInfoId).roomId(roomSubmitVo.getId()).build();
                labelInfos.add(roomLabelBuilder);
            }
            roomLabelService.saveBatch(labelInfos);
        }

        //支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)) {
            ArrayList<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType build = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                roomPaymentTypes.add(build);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypes);
        }
        //可选租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)) {
            ArrayList<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm build = RoomLeaseTerm.builder().leaseTermId(roomSubmitVo.getId()).roomId(leaseTermId).build();
                roomLeaseTerms.add(build);
            }
            roomLeaseTermService.saveBatch(roomLeaseTerms);
        }
    }
}




